package com.example.myapplication.routertest

import androidx.compose.runtime.Composable
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.dialog
import androidx.navigation.compose.navigation
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import androidx.navigation.toRoute

@Composable
fun Router() {
    val navController = rememberNavController() // 创建 NavController
    NavHost(
        navController = navController,
        startDestination = HomeRoute
    ) {
        // 普通路由
        composable<HomeRoute> { Home(navController) }
        composable<Page1Route> { Page1(navController) }
        composable<Page2Route> { Page2(navController) }
        // 携带路径参数
        composable(
            route = SafeRoute.User.route,
            arguments = SafeRoute.User.args()
        ) { navBackStackEntry ->
            val id = navBackStackEntry.arguments?.getInt(SafeRoute.User.ID) ?: 0
            User(
                id = id,
                navController,
            )
        }
        // 携带查询参数
        composable<StudentRoute> { navBackStackEntry ->
            val args = navBackStackEntry.toRoute<StudentRoute>()
            Student(
                stuName = args.stuName,
                navController
            )
        }

        // 弹框路由
        dialog<Dialog1Route> {
            Dialog1(navController)
        }

        // 嵌套路由
        navigation<DetailRoute>(
            startDestination = DetailRoute.Detail1Route
        ) {
            composable<DetailRoute.Detail1Route> { Detail1(navController) }
        }
    }
}